package com.yitong.equipment.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yitong.equipment.R
import com.yitong.equipment.entity.other.MineConfigBean
import com.yitong.equipment.entity.other.MineConfigColumnBadge
import com.yitong.equipment.entity.other.MineConfigColumnBean
import java.util.HashMap

class MineAdapter(data: MutableList<MineConfigBean>? = null) : BaseQuickAdapter<MineConfigBean, BaseViewHolder>(R.layout.item_mine_layout, data) {
    var columnViewMap: MutableMap<String, View>? = null
    var columnClickListener: MineColumnClickListener? = null

    override fun convert(helper: BaseViewHolder, item: MineConfigBean) {
        val tvHeadName: TextView = helper.getView(R.id.tv_head_name)
        tvHeadName.text = item.groupName
        initColumnView(helper.getView(R.id.column_container), item.columnList)
    }

    open fun initColumnView(container: ViewGroup?, columnList: List<MineConfigColumnBean>?) {
        if (columnList != null && container != null) {
            columnViewMap = HashMap()
            container.removeAllViews()
            for (i in columnList.indices) {
                val columnBean: MineConfigColumnBean = columnList[i]
                val columnView: View = LayoutInflater.from(container.context).inflate(R.layout.item_mine_column, container, false)
                val lp = columnView.layoutParams
                lp.width = (ScreenUtils.getScreenWidth() - container.paddingLeft - container.paddingRight - SizeUtils.dp2px((16 * 4).toFloat())) / 4
                lp.height = ViewGroup.LayoutParams.WRAP_CONTENT
                columnView.layoutParams = lp
                (columnView.findViewById<View>(R.id.tv_mine_column) as TextView).text = columnBean.columnName
                (columnView.findViewById<View>(R.id.iv_mine_column) as ImageView).setImageResource(columnBean.columnIcon)
                initBadge(columnView, columnBean.badge)
                columnView.setOnClickListener {
                    columnClickListener?.onMineColumnClick(columnBean.columnTag, columnBean)
                }
                columnViewMap!![columnBean.columnTag] = columnView
                container.addView(columnView)
            }
        }
    }

    open fun initBadge(columnView: View, badge: MineConfigColumnBadge?) {
        val ivPoint = columnView.findViewById<ImageView>(R.id.iv_column_point)
        val tvCount = columnView.findViewById<TextView>(R.id.tv_column_count)
        if (badge != null) {
            when (badge.type) {
                1 -> {
                    showOrHide(ivPoint, true)
                    showOrHide(tvCount, false)
                }
                2 -> {
                    val count: Int = badge.count
                    showOrHide(ivPoint, false)
                    showOrHide(tvCount, count > 0)
                    tvCount.text = if (count > 0) if (count > 99) "99+" else count.toString() else ""
                }
                0, 3 -> {
                    showOrHide(ivPoint, false)
                    showOrHide(tvCount, false)
                }
                else -> {
                    showOrHide(ivPoint, false)
                    showOrHide(tvCount, false)
                }
            }
        } else {
            showOrHide(ivPoint, false)
            showOrHide(tvCount, false)
        }
    }

    open fun showOrHide(v: View, b: Boolean) {
        v.visibility = if (b) View.VISIBLE else View.INVISIBLE
    }

    interface MineColumnClickListener {
        fun onMineColumnClick(tag: String?, columnBean: MineConfigColumnBean?)
    }

    fun setColumnOnClickListener(columnClickListener: MineColumnClickListener) {
        this.columnClickListener = columnClickListener
    }

    fun updateColumnBadge(columnBean: MineConfigColumnBean?) {
        if (columnBean != null && columnViewMap != null) {
            val columnView = columnViewMap!![columnBean.columnTag]
            if (columnView != null) {
                initBadge(columnView, columnBean.badge)
            }
        }
    }
}